/*!
 * 下拉列表控件.
 * ecp.qzz.ui.drop.poplistview.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.pwindow","qzz.listview", "qzz.iedit"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.drop");
	/**
	 * 下拉列表控件.
	 */
	qzz.ui.drop.poplistview = qzz.Class(qzz.ui.drop.popwindow, {
		/**
		 * 创建.
		 */
		_create:function() {
		    this.base();
		    this.className = "PopListView";
		    this.logInfo("popListView.create");
	    	this.multiSelect = this.getBooleanValue(this.getOption().multiple, false);
	    	this.multiSelect = this.getBooleanValue(this.getOption().multiSelect, this.multiSelect);
	    	this.selectAll = this.getBooleanValue(this.getOption().selectAll, this.selectAll);
	    	this.getOption().idkey = this.getOption().idkey || "id";
	    	this.getOption().capkey = this.getOption().capkey || "text";
	    	this.getOption().search = this.getBooleanValue(this.getOption().search, false);
	    	this.getOption().more = this.getBooleanValue(this.getOption().more, false);
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {
	    	this.base();
	    	//this.thisWindow.style.borderWidth = "0px";
	    	if(this.getOption().search === true || this.getOption().more === true) {
	    		this.popTable = this.createElement("TABLE");
	    		this.popView = this.popTable.insertRow(0);
	    		if(this.getOption().search === true) {
		    		var tr = this.popTable.insertRow(0);
		    		var td = tr.insertCell(0);
		    		this.searchBox = new qzz.ui.input.edit(null, {"width":100,placeholder:this.getI18n("qzz.inputKey", "输入关键字"),"margin":4});
		    		this.searchBox.setParent(td);
		    		var _this = this;
		    		this.searchBox.bind("onChange",function(text) {
		    			_this.clearFilter();
		    			if(text != null && text !== "") {
			    		    _this.addFilter(text);
		    			} else {
		    				_this.view.refresh(text);
		    				_this.refresh();		    				
		    			}
		    		});
	    		}
	    		if(this.getOption().more === true) {
	    			var tr = this.popTable.insertRow(this.popTable.rows.length);
	    			var td = tr.insertCell(0);
	    			var a = this.createElement("A");
	    			this._setHtml(a, "更多");
	    			a.href = "#";
	    			var _this = this;
	    			a.onclick = function() {
	    				_this.trigger("onMoreClick", []);
	    			}
					a.style["float"] = "right";
	    			a.style["marginRight"] = "4px";
	    			td.appendChild(a);
	    		}
	    		this.thisWindow.appendChild(this.popTable);
	    	}
	    	//列表控件
	    	this.view = new qzz.ui.listview(undefined, 
	    		{"cssPrefix":this._cssPrefix,
	    		 "data":this.getOption().data,
	    		 "selectAll":this.selectAll,
	    		 "multiSelect":this.multiSelect,
	    		 "fontSize":this.getOption().fontSize,
	    		 "scrollFireEvent":false,
	    		 "idkey":this.getOption().idkey, 
	    		 "leafOnly":this.getOption().leafOnly,
	    		 "capkey":this.getOption().capkey,
	    		 "border":"0px",
	    		 "rowHeight":this.getOption().rowHeight,
	    		 "viewType":this.getOption().viewType
	    		});	    	
	    	var _this = this;
	    	this.view.autoHeight = true;
	    	this.view._this = this;
	    	this.view.bind("onHeightChanged", function(height) {
	    		this._this.setHeight(height);
	    	});
	    	this.logInfo("popListView.render");
	    	this.refresh();
	    },
	    refreash:function() {
	    	this.refresh();
	    },
	    /**
	     * 刷新.
	     */
	    refresh:function() {
	    	this.view.setParent(this.getDropWindow());
		    this.setWidth(this.view.getWidth() + 2, false);
		    if(this.searchBox != null) {
		    	this.searchBox.setWidth(this.view.getWidth() - 8);
		    }
		    this.setHeight(this.view.getHeight() + 2);
		    this.view.onSelect = this.doSelect;
	    },
	    getDropWindow:function() {
	    	if(this.getOption().search === true || this.getOption().more === true) {
	    		return this.popView;
	    	} else {
	    		return this.base();
	    	}
	    },
	    /**
	     * 失去焦点事件.
	     */
	    doBlur:function() {
	    	this.base();    	
	    	if(this.view != null) {
	    		this.view.setBlur();
	    	}
	    },
	    /**
	     * 选择.
	     * @param index 序号 
	     * @param item 节点
	     */
	    doSelect:function(index, item) {
	    	var _this = this._this;
	    	if(_this.view.focus) {	
	    		if(_this.multiSelect != true) {
		    	    _this.hide();
		    	    _this.view.setBlur();
	    		}		    	
		    	//执行选择事件.
		    	if(typeof _this.onSelect == "function") {
		    		_this.onSelect(index, item);	
		    	}
	    	}
	    },
	    /**
	     * 鼠标滚动事件.
	     * @param ev 事件对象
	     */
	    doMouseWheel:function(ev) {
	    	//this.logInfo("window.mouseWheel");
	    	if(!this.view.focus) {
	    		this.view.doMouseWheel(ev);
	    	}
	    }, 
	    /**
	     * 键盘弹起事件.
	     * @param ev 事件对象
	     * @param key 键值
	     */
	    doKeyUp:function(ev, key) {
	    	if(this.focus) {
	    		this.view.setFocus();
	    	}
	    },
	    /**
	     * 设置宽度.
	     * @param value 值
	     */
	    setWidth:function(value, doRs) {
	    	this.base(value, doRs);
	    	this.view.setWidth(value - 2, doRs);
	    	if(this.searchBox != null) {
		    	this.searchBox.setWidth(this.view.getWidth() - 8);
		    }
	    },
	    setHeight:function(value, doRs) {
	    	var sh = 0;
    		if(this.searchBox != null) {
	    	    sh = this.searchBox.getHeight() + 8;
	        }
    		if(this.getOption().more === true) {
    			sh += 20;
    		}
	    	this.base(value + sh, doRs);
	    },
	    isFocus:function() {
	    	return this.focus && this.view.focus;
	    },
	    /**
	     * 上一条.
	     */
	    prior:function() {
	    	this.view.prior();
	    },
	    /**
	     * 下一条.
	     */
	    next:function() {
	    	this.view.next();
	    },
	    /**
	     * 判断字符串是否存在.
	     * @param text 字符串
	     */
	    indexOfText:function(text, filter) {
	    	return this.view.indexOfText(text, filter);
	    },
	    /**
	     * 判断值是否存在.
	     * @param value 值
	     */
	    indexOf:function(value, filter) {
	    	return this.view.indexOf(value, filter);
	    },
	    /**
	     * 获取节点.
	     * @param index 序号
	     * @return 返回节点
	     */
	    getItem:function(index) {
	    	if(index == null) {
	    	    return this.view.getCurItem();
	    	} else {
	    		return this.view.getItem(index);
	    	}
	    },
	    /**
	     * 获取选择序号.
	     * @return 返回序号
	     */
	    getSelectIndex:function() {
	    	return this.view.getSelectIndex();
	    },
	    /**
	     * 设置复选.
	     * @param {boolean} Value 复选类型
	     */
	    setMultiSelect:function(value) {
	    	this.multiSelect = value;
	    	this.view.multiSelect = value;
	    },
		/**
	     * 设置复选.
	     * @param {boolean} Value 复选类型
	     */
	    setSelectAll:function(value) {
	    	this.selectAll = value;
	    	this.view.selectAll = value;
	    },
	    /**
	     * 执行下拉.
	     * @param top 顶点
	     * @param left 左边
	     */
	    execute:function(top, left, pHeight) {
	    	if(this.view.autoHeight) {
	    		this.setHeight(this.view.getHeight() + 2, false);
	    		this.setWidth(this.view.getWidth() + 2);
	    		if(this.searchBox != null) {
	    			//清空
	    			this.searchBox.setValue("");
	    			this.searchBox.setFocus();
	    		}
	    	}
	    	this.base(top, left, pHeight);
	    	this.view.setFocus();
	    	this.view.refreshScrollBar();
	    },
	    /**
	     * 设置列表.
	     * @param itemList
	     */
	    setItemList:function(itemList) {
	    	this.view._update = false;
	    	this.view.setItemList(itemList);
	    	this.view._update = true;
	    	if(this.isShow()) {
	    		this.view.endUpdate();
	    		this.refresh();
	    	}
	    },
	    /**
	     * 添加节点.
	     * @param item 节点
	     */
	    addItem:function(item) {
	    	this.view.addItem(item);
	    },
	    /**
	     * 删除节点.
	     * @param index 序号
	     */
	    delItem:function(index) {
	    	this.view.delItem(item);
	    },
	    /**
	     * 设置过滤.
	     * @param text 条件
	     */
	    addFilter:function(text, done) {
	    	if(text !== "" && !this.view.filterExist(text)) {
		    	this.view.addFilter(text, done);
		    	if(done != false) {
			    	this.setWidth(this.view.getWidth() + 2, false);
				    this.setHeight(this.view.getHeight() + 2);
		    	}
	    	}
	    },
	    /**
	     * 执行过滤
	     * @name doFilter
	     */
	    doFilter:function() {
	    	this.view.doFilter();
	    	this.setWidth(this.view.getWidth() + 2, false);
		    this.setHeight(this.view.getHeight() + 2);
	    },
	    /**
	     * 清除过滤条件.
	     * @name clearFilter
	     */
	    clearFilter:function() {
	    	this.view.clearFilter();
	    },
	    /**
	     * 返回过滤条件.
	     * @name getFilter
	     * @return {string} 过滤字符串.
	     */
	    getFilter:function() {
	    	return this.view.getFilter();
	    },
	    /**
	     * 通过字符串选择节点.
	     * @param {string} text 字符串
	     * @return 返回选择的列表
	     */
	    setSelectByText:function(Text) {
	    	return this.view.setSelectByText(Text);
	    },
	    /**
	     * 通过值选择节点.
	     * @param value 值
	     */
	    setSelect:function(value) {
	    	this.view.setSelect(value);
	    },
	    /**
	     * 获取选择列表.
	     * @name getSelect
	     * @return 返回选择列表.
	     */
	    getSelect:function() {
	    	return this.view.getSelect();
	    },
	    /**
	     * 
	     */
	    length:function() {
	    	return this.view.length();
	    },
	    setIdkey:function(key) {
        	this.getOption().idkey = key;
        	this.view.setIdkey(key);
        },
        clearSelectAll:function() {
        	this.view.clearSelectAll();
        }
	    
	});
	_bindRenderFunc("qzzpoplistview", qzz.ui.drop.poplistview);
	return qzz.ui.drop.poplistview;
});
	